package org.zhouhe.modules.hotproject.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.zhouhe.base.BaseService;
import org.zhouhe.page.PageUtil;
import org.zhouhe.vo.HotProject;
import org.zhouhe.vo.HotProjectRegion;
import org.zhouhe.vo.Investment;
import org.zhouhe.vo.Region;

@Service
@SuppressWarnings("unchecked")
public class HotProjectService extends BaseService {
	public HotProject getHotProjectDetail(HotProject hotProject) {
		return (HotProject) dao.get(HotProject.class, hotProject.getHotProjectId());
	}

	public PageUtil getHotProjectList(PageUtil pageUtil, HotProject hotProject, Integer regionId, Integer industryId, Integer investmentId) {
		if (pageUtil == null) {
			pageUtil = new PageUtil();
		}
		List<Object> paramList = new ArrayList<Object>();
		StringBuilder from = new StringBuilder();
		String select = "select distinct hp ";
		from.append(" from HotProject hp ");
		from.append(" left join hp.hotProjectRegions pr ");
		from.append(" where hp.status=1  ");
		if (hotProject != null) {
			// 关键字：
			String title = hotProject.getTitle();
			if (title != null && !"".equals(title.trim())) {
				from.append(" and hp.title like ? ");
				paramList.add("%" + title + "%");
			}
		}
		// 地区：
		if (regionId != null) {
			from.append(" and pr.region.regionId=? ");
			paramList.add(regionId);
		}
		// 行业：
		if (industryId != null) {
			from.append(" and hp.industry.idPath like ? ");
			paramList.add("%|"+ industryId + "|%");
		}
		// 投资总额：
		if (investmentId != null) {
			from.append(" and hp.investment.investmentId=? ");
			paramList.add(investmentId);
		}
		pageUtil.setHQL(select, from.toString(), paramList);
		return dao.findByPage(pageUtil);
	}

	public List<Region> getRegionList() {
		return dao.find("from Region where 1=1 order by regionId asc");
	}

	public List<HotProject> getHotProjectList(Integer regionId, Integer industryId, Integer count) {
		PageUtil pageUtil = new PageUtil();
		pageUtil.setPageSize(count);
		this.getHotProjectList(pageUtil, null, regionId, industryId, null);
		return (List<HotProject>) pageUtil.getPageData();
	}

	public void save(HotProject hotProject, List<Integer> regionIdList) {
		hotProject.setCreateTime(new Timestamp(new Date().getTime()));
		hotProject.setStatus(1);
		dao.save(hotProject);
		for (Integer regionId : regionIdList) {
			Region region = new Region();
			region.setRegionId(regionId);
			HotProjectRegion pr = new HotProjectRegion();
			pr.setHotProject(hotProject);
			pr.setRegion(region);
			dao.save(pr);
		}
	}

	public void update(HotProject hotProject, List<Integer> regionIdList) {
		dao.executeUpdate("delete from HotProjectRegion where hotProject.hotProjectId=" + hotProject.getHotProjectId());
		HotProject hotProjectBean = (HotProject) dao.get(HotProject.class, hotProject.getHotProjectId());
		hotProjectBean.setAddress(hotProject.getAddress());
		hotProjectBean.setContent(hotProject.getContent());
		hotProjectBean.setDisplayTime(hotProject.getDisplayTime());
		hotProjectBean.setIndustry(hotProject.getIndustry());
		hotProjectBean.setPrice(hotProject.getPrice());
		hotProjectBean.setTitle(hotProject.getTitle());
		hotProjectBean.setUpdateTime(new Timestamp(new Date().getTime()));
		hotProjectBean.setInvestment(hotProject.getInvestment());
		dao.update(hotProjectBean);
		for (Integer regionId : regionIdList) {
			Region region = new Region();
			region.setRegionId(regionId);
			HotProjectRegion pr = new HotProjectRegion();
			pr.setHotProject(hotProjectBean);
			pr.setRegion(region);
			dao.save(pr);
		}
	}

	public void delete(HotProject hotProject) {
		hotProject = (HotProject) dao.get(HotProject.class, hotProject.getHotProjectId());
		hotProject.setStatus(0);
		dao.update(hotProject);
	}
	
	public List<Investment> getInvestmentList(){
		return dao.find("from Investment where status=1 order by investmentId ASC");
	}
}
